package blog.wls.jmx.appmbean.client;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.MBeanInfo;

import javax.management.remote.JMXConnector;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnectorFactory;
     
import java.util.Hashtable;
import java.util.Set;
import java.util.Locale;

public class JMXClient {

    public static void main(String[] args) throws Exception {

        JMXConnector jmxCon = null;
 
        try {
      
            JMXServiceURL serviceUrl = 
                new JMXServiceURL(
                "service:jmx:iiop://127.0.0.1:7001/jndi/weblogic.management.mbeanservers.runtime"); 

            System.out.println("Connecting to: " + serviceUrl);
 
            // properties associated with the connection
            Hashtable env = new Hashtable();
 
            // Specifies WLS JMX protocol provider package
            env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
 
            String[] credentials = new String[2];
            credentials[0] = "weblogic";
            credentials[1] = "weblogic";
            env.put(JMXConnector.CREDENTIALS, credentials);

            env.put("weblogic.management.remote.locale", Locale.FRENCH);

            // We get an instance of the JMXConnector interface
            jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, env);
 
            // We finally connect to the WLS instance defined  in the JMXServiceURL
            jmxCon.connect();

            MBeanServerConnection con = jmxCon.getMBeanServerConnection();
 
            Set<ObjectName> mbeans = 
                con.queryNames(new ObjectName("blog.wls.jmx.appmbean:name=myAppProperties,type=PropertyConfig,*"), null);

            for (ObjectName mbeanName : mbeans) {
                System.out.println("\n\nMBEAN: " + mbeanName);

                MBeanInfo minfo = con.getMBeanInfo(mbeanName);

                System.out.println("MBean Description: "+minfo.getDescription());
                System.out.println("\n");
            }

        }
        finally {
            // release the connection
            if (jmxCon != null)
                jmxCon.close();
        }


    }
}
